package com.gs.common.manager

import android.app.Activity
import java.util.concurrent.CopyOnWriteArrayList

object ActivityManager {

    private val mActivitys = CopyOnWriteArrayList<Activity>()

    fun pushActivity(activity: Activity) {
        mActivitys.add(activity)
    }

    fun popActivity(activity: Activity) {
        mActivitys.remove(activity)
    }

    fun currentActivity(): Activity? {
        return if (mActivitys.isEmpty()) null else mActivitys[mActivitys.size - 1]
    }

    fun finishCurrentActivity() {
        if (mActivitys.isNotEmpty()) {
            val activity = mActivitys[mActivitys.size - 1]
            finishActivity(activity)
        }
    }

    fun finishActivity(activity: Activity?) {
        if (activity != null) {
            mActivitys.remove(activity)
            activity.finish()
        }
    }

    fun finishAllActivity() {
        if (mActivitys.size > 0) {
            for (activity in mActivitys) {
                finishActivity(activity)
            }
        }
    }

    fun finishAllActivityExceptHomeActivity() {
        if (mActivitys.size > 0) {
            for (activity in mActivitys) {
                if (activity.toString().contains("HomeActivity")) continue
                finishActivity(activity)
            }
        }
    }
}